//入口函数
$(function (){
    var listUrl = '/shopadmin/getproductlist?pageIndex=1&pageSize=9999';
    var changeStatusURL = '/shopadmin/changestatus';

    getList();

    function getList(){
        $.getJSON(listUrl,function (data){
            if (data.success){
                var productList = data.productList;
                var tempHtml = '';
                productList.map(function (item,index){
                    var textOp = "下架";
                    var contraryStatus = 0;
                    if (item.enableStatus == 0){
                        textOp = "上架";
                        contraryStatus = 1;
                    }else{
                        contraryStatus = 0;
                    }
                    tempHtml += '' + '<div class="row row-product">'
                        + '<div class="col-33">'
                        + item.productName
                        + '</div>'
                        + '<div class="col-33">'
                        + item.priority
                        + '</div>'
                        + '<div class="col-33">'
                        + '<a href="#" class="edit" data-id="'
                        + item.productId
                        + '" data-status="'
                        + item.enableStatus
                        + '">编辑</a>'
                        + '<a href="#" class="status" data-id="'
                        + item.productId
                        + '" data-status="'
                        + contraryStatus
                        + '">'
                        + textOp
                        + '</a>'
                        + '<a href="#" class="preview" data-id="'
                        + item.productId
                        + '" data-status="'
                        + item.enableStatus
                        + '">预览</a>'
                        + '</div>'
                        + '</div>';
                });
                $('.product-wrap').html(tempHtml);
            }
        });
    }

    /**
     * 下架的操作
     */
    function changeStatus(id,enableStatus){
        var product = {};
        product.productId = id;
        product.enableStatus = enableStatus;
        $.confirm('确定吗？',function (){
            $.ajax({
                url : changeStatusURL,
                type : 'POST',
                data : {
                    productStr : JSON.stringify(product)
                },
                dataType : 'json',
                success : function (data){
                    if (data.success){
                        $.toast(data.success);
                        getList();
                    }else{
                        $.toast(data.errMsg);
                    }
                }
            });
        });
    }

    $('.product-wrap').on(
        'click',
        'a',
        function(e) {
            var target = $(e.currentTarget);
            if (target.hasClass('edit')) {
                window.location.href = '/shopadmin/productoperation?productId='
                    + e.currentTarget.dataset.id;
            } else if (target.hasClass('status')) {
                changeStatus(e.currentTarget.dataset.id,
                    e.currentTarget.dataset.status);
            } else if (target.hasClass('preview')) {
                // TODO
                window.location.href = '/frontend/productdetail?productId='
                    + e.currentTarget.dataset.id;
            }
        });

    $('#new').click(function (){
        window.location.href = '/shopadmin/productoperation';
    });


})